<?php

namespace haibao\jiepai\business;

use haibao\jiepai\business\notification\ios\IOSBroadcast;
use haibao\jiepai\business\notification\ios\IOSGroupcast;
use haibao\jiepai\business\notification\android\AndroidBroadcast;
use haibao\jiepai\business\notification\android\AndroidGroupcast;
use haibao\jiepai\data\Message as MessageData;

class UPush extends Base{
	protected static $appkey           = NULL; 
	protected static $appMasterSecret     = NULL;
	protected static $timestamp        = NULL;
	protected static $validation_token = NULL;
	protected static $production_mode = 'false';

	public static function sendPush($pushModel){
		if(!$pushModel->IsOnlyUser){
			//所有用户的推送  苹果用户
			if ($pushModel->RangeDevice == 0 || $pushModel->RangeDevice == 1) {
				self::sendIOSPush($pushModel);
			}
			//所有用户的推送  安卓用户
			if ($pushModel->RangeDevice == 0 || $pushModel->RangeDevice == 2) {
				self::sendAndroidPush($pushModel);
			}
		}else {
			//注册用户的推送  苹果用户
			if ($pushModel->RangeDevice == 0 || $pushModel->RangeDevice == 1) {
				self::sendIOSPushRegister($pushModel);
			}
			//注册用户的推送  安卓用户
			if ($pushModel->RangeDevice == 0 || $pushModel->RangeDevice == 2) {
				self::sendAndroidPushRegister($pushModel);
			}
		}
	}
	
	
	public static function sendIOSPush($pushModel){
		self::$appkey = '5267bccf56240b96c20119ee';
		self::$appMasterSecret = 'fqd6zyds7vdabxmlyikul0lpqwddvkdg';
		self::$timestamp = strval(time());
		$brocast = new IOSBroadcast();
		$brocast->setAppMasterSecret(self::$appMasterSecret);
		$brocast->setPredefinedKeyValue("appkey",self::$appkey);
		$brocast->setPredefinedKeyValue("timestamp",self::$timestamp);
		$brocast->setPredefinedKeyValue("production_mode",self::$production_mode);//测试环境 false   true为实际环境
		$brocast->setPredefinedKeyValue("alert", $pushModel->Message);
		
		//payload中的数据  另外的type="broadcast已传在IOSBroadcast中"
		$brocast->setCustomizedField("type", $pushModel->TargetView);
		$brocast->setCustomizedField("ArticleID", $pushModel->ArticleID);
		$brocast->setCustomizedField("ArticleType", $pushModel->ArticleType);
		
		
		$brocast->setPredefinedKeyValue("description", 'cms--'.$pushModel->Message);
			
		if($pushModel->PublishTime){
			$dateTime = $pushModel->PublishTime.':00';
			$brocast->setPredefinedKeyValue("start_time", $dateTime);
		}
		
		$brocast->send();
	}
	
	/**
	 * ios的注册用户
	 */
	public static function sendIOSPushRegister($pushModel){
		self::$appkey = '5267bccf56240b96c20119ee';
		self::$appMasterSecret = 'fqd6zyds7vdabxmlyikul0lpqwddvkdg';
		self::$timestamp = strval(time());

		$groupcast = new IOSGroupcast();
		$groupcast->setAppMasterSecret(self::$appMasterSecret);
		$groupcast->setPredefinedKeyValue("appkey",self::$appkey);
		$groupcast->setPredefinedKeyValue("timestamp",self::$timestamp);
		$groupcast->setPredefinedKeyValue("production_mode",self::$production_mode);//测试环境 false   true为实际环境
		$groupcast->setPredefinedKeyValue("alert", $pushModel->Message);
		$groupcast->setPredefinedKeyValue("description", 'cms--'.$pushModel->Message);
			
		if($pushModel->PublishTime){
			$dateTime = $pushModel->PublishTime.':00';
			$groupcast->setPredefinedKeyValue("start_time", $dateTime);
		}
		
		$filter = 	array(
				"where" => 	array(
						"and" 	=>  array(
								array(
										"tag" => "registered_user"
								)
						)
				)
		);
		$groupcast->setPredefinedKeyValue("filter", $filter);
		//payload中的数据  另外的type="broadcast已传在IOSBroadcast中"
		$groupcast->setCustomizedField("type", $pushModel->TargetView);
		$groupcast->setCustomizedField("ArticleID", $pushModel->ArticleID);
		$groupcast->setCustomizedField("ArticleType", $pushModel->ArticleType);
			
		$groupcast->send();

	}
	
	/**
	 * 安卓的推送 全部用户
	 */
	public static function sendAndroidPush($pushModel){
		self::$appkey = '5267becb56240b96d6011c64';
		self::$appMasterSecret = 'j8uqnhuui17lnrbbdhoetylvbmzufhkl';
		self::$timestamp = strval(time());
	
		$brocast = new AndroidBroadcast();
		$brocast->setAppMasterSecret(self::$appMasterSecret);
		$brocast->setPredefinedKeyValue("appkey",           self::$appkey);
		$brocast->setPredefinedKeyValue("timestamp",        self::$timestamp);
		$brocast->setPredefinedKeyValue("production_mode",self::$production_mode);//测试环境 false   true为实际环境
		
		$brocast->setPredefinedKeyValue("ticker",           $pushModel->Message);
		$brocast->setPredefinedKeyValue("title",            "街拍");
		$brocast->setPredefinedKeyValue("text",             $pushModel->Message);
		$brocast->setPredefinedKeyValue("after_open",       "go_custom");
		// Set 'production_mode' to 'false' if it's a test device.
		// For how to register a test device, please see the developer doc.
		$brocast->setPredefinedKeyValue("custom", "{type: .".$pushModel->TargetView.", ArticleID: ".$pushModel->ArticleID.", ArticleType:".$pushModel->ArticleType."}");
		
		$brocast->setPredefinedKeyValue("description", 'cms--'.$pushModel->Message);
			
		if($pushModel->PublishTime){
			
			$dateTime = $pushModel->PublishTime.':00';
			$brocast->setPredefinedKeyValue("start_time", $dateTime);
		}
		
		
		$brocast->send();
			
		
		
	}
	
	/**
	 * 安卓的注册用户
	 */
	public static function sendAndroidPushRegister($pushModel){
		self::$appkey = '5267becb56240b96d6011c64';
		self::$appMasterSecret = 'j8uqnhuui17lnrbbdhoetylvbmzufhkl';
		self::$timestamp = strval(time());
		
	
		
		$filter = 	array(
				"where" => 	array(
						"and" 	=>  array(
								array(
										"tag" => "registered_user"
								),
						)
				)
		);
		
		$groupcast = new AndroidGroupcast();
		$groupcast->setAppMasterSecret(self::$appMasterSecret);
		$groupcast->setPredefinedKeyValue("appkey",           self::$appkey);
		$groupcast->setPredefinedKeyValue("timestamp",        self::$timestamp);
		$groupcast->setPredefinedKeyValue("production_mode",self::$production_mode);//测试环境 false   true为实际环境
			
		$groupcast->setPredefinedKeyValue("ticker",           $pushModel->Message);
		$groupcast->setPredefinedKeyValue("title",            "街拍");
		$groupcast->setPredefinedKeyValue("text",             $pushModel->Message);
		$groupcast->setPredefinedKeyValue("after_open",       "go_custom");
		// Set 'production_mode' to 'false' if it's a test device.
		// For how to register a test device, please see the developer doc.
		$groupcast->setPredefinedKeyValue("custom", "{type: .".$pushModel->TargetView.", ArticleID: ".$pushModel->ArticleID.", ArticleType:".$pushModel->ArticleType."}");
			
		$groupcast->setPredefinedKeyValue("description", 'cms--'.$pushModel->Message);
	
		if($pushModel->PublishTime){
			$dateTime = $pushModel->PublishTime.':00';
			$groupcast->setPredefinedKeyValue("start_time", $dateTime);
		}
		$groupcast->setPredefinedKeyValue("filter", $filter);
		
		
		$groupcast->send();
			
		
	}
	
	public static function pushWelcomeMsg($userId, $visitorId){
		if (!$userId && !$visitorId) return;
		$data = new MessageData();
		return $data->pushWelcomeMsg($userId,$visitorId);
	}
	
	
}